package com.jackiechancinema.ssm.service.Impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jackiechancinema.ssm.bo.DepartmentBo;
import com.jackiechancinema.ssm.config.SystemContant;
import com.jackiechancinema.ssm.dao.DepartmentDao;
import com.jackiechancinema.ssm.dto.DepartmentDto;
import com.jackiechancinema.ssm.entity.Department;
import com.jackiechancinema.ssm.listener.DepartmentReadListener;
import com.jackiechancinema.ssm.service.Inte.DepartmentService;
import com.jackiechancinema.ssm.vo.PageVo;
import com.jackiechancinema.ssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Repository
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentDao dao;
    @Autowired
    private DepartmentReadListener listener;

    @Override
    public R insert(Department department) {
        int i=dao.insert(department);
        if (i>0){
            return R.ok(null);
        }else{
            return R.fail();
        }
    }

    @Override
    public R delete(int depart_id) {
        int i=dao.delete(depart_id);
        if (i>0){
            return R.ok(null);
        }else{
            return R.fail();
        }

    }

    @Override
    public R update(Department department) {
        int i=dao.update(department);
        if (i>0){
            return R.ok(null);
        }else{
            return R.fail();
        }
    }
    @Override
    public PageVo<Department> page(int page, int limit) {
        //开启分页
        PageHelper.startPage(page,limit);
//        System.out.println(dao.selectAll());
        //
        PageInfo<Department> pageInfo=new PageInfo<>(dao.queryAll());
        PageVo<Department> pageVo = new PageVo<Department>(SystemContant.PAGE_R,"OK",pageInfo.getTotal(),pageInfo.getList());
        return pageVo;
    }

    @Override
    public PageVo<Department> page2(DepartmentBo bo) {
        Department department = new Department();
        if (bo!=null){
            if (StringUtils.hasLength(bo.getDname())){
                department.setDname(bo.getDname());
            }
            PageHelper.startPage(bo.getPage(), bo.getLimit());
            PageInfo<Department> pageInfo = new PageInfo<Department>(dao.queryBy(department));
            return new PageVo<Department>(SystemContant.PAGE_R,"OK",pageInfo.getTotal(),pageInfo.getList());
        }
        return new PageVo<Department>(SystemContant.PAGE_R,"OK",0,null);
    }

    /**
     * 批量新增
     * @param file
     * @return
     */
    @Override
    public R insertBatch(MultipartFile file) {
        if (!file.isEmpty()){
            try {
//                System.out.println(file.getName());
                EasyExcel.read(file.getInputStream(), DepartmentDto.class,listener).sheet().doRead();
                return R.ok(null);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return R.fail();
    }

    /**
     * 导出
     * @param bo
     * @param response
     */
    @Override
    public void exportExcel(DepartmentBo bo, HttpServletResponse response) {
        //1.设置响应消息头，目的：告诉请求方，这个是下载
        String fn=System.currentTimeMillis()+".xlsx";
        //设置响应头  告知浏览器，要以附件的形式保存内容   filename=浏览器显示的下载文件名
        response.setHeader("content-disposition","attachment;filename="+fn);
//        System.out.println(bo.getDname());
        //2.组装查询条件
        Department department=new Department();
        if(StringUtils.hasLength(bo.getDname())){
            department.setDname(bo.getDname());
        }

        //3.获取要下载的数据
        List<Department> list=dao.queryBy(department);
        try {
            //4.生成Excel，并将数据写出到响应流中
            EasyExcel.write(response.getOutputStream(),Department.class).sheet().doWrite(list);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
